package com.shiguiwu.springmybatis.algorithm.hashtal;

/**
 * @description: 自定义hashtable
 * @author: stone
 * @date: Created by 2021/5/29 20:42
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis
 */
public class MyHashTable {


    public static void main(String[] args) {
        MyHashTable hashTable = new MyHashTable(2);
        hashTable.add(new Emp(1, "张三"));
        hashTable.add(new Emp(2, "张三"));
        hashTable.add(new Emp(0, "张三"));
        hashTable.add(new Emp(3, "张三"));
        hashTable.list();

    }

    private HashList[] hashLists;

    private int size;

    public MyHashTable(int size) {
        this.size = size;
        hashLists = new HashList[size];
        for (int i = 0; i < size; i++) {
            hashLists[i] = new HashList();
        }
    }

    public void add(Emp emp) {
        int i = computer(emp);
        hashLists[i].add(emp);

    }

    public void list() {
        for (int i = 0; i < hashLists.length; i++) {
            hashLists[i].list();
            System.out.println("第" + i + "个链表打印接受===========>");

        }
    }


    public int computer(Emp emp) {
        return emp.id % (this.size);
    }

    public static class Emp {

        private Integer id;

        private String name;

        private Emp next;



        public Emp(Integer id, String name) {
            this.id = id;
            this.name = name;
        }




        }

    public static class HashList {
        private Emp header;

        public void add(Emp emp) {
            if (header == null) {
                header = emp;
                return;
            }
            Emp t = header;
            while (t.next!= null) {
                t = t.next;

            }
            t.next = emp;
        }

        public void list() {
            if (header == null) {
                System.out.println("当前列表为null");
                return;

            }
            Emp t = header;
            while (t != null) {
                System.out.print(String.format("当前员工id为%s ===>名称为%s", t.id, t.name));
                t = t.next;
            }
        }


    }
}
